import traceback

from absl import flags

import coin.exchange.kr_rest.order.order_impl as order_impl

from coin.support.accounting.logic.account_proto_util import (clear_zero_balance,
                                                              clear_zero_position)

from coin.support.accounting.logic.key_util import get_key_file
from coin.support.accounting.logic.query_util import multithread_request_executor
from xunkemgmt_client.support.accounting.database.db_output import (
    DBOutputTransfer, DBOutputBalance, DBOutputPosition)


FLAGS = flags.FLAGS

flags.DEFINE_bool('traceback', False, 'exception traceback')

flags.DEFINE_bool('verbose', False, 'verbose')


def query_balance_list(request_list, proxy=None):
  def f(request):
    if FLAGS.verbose:
      print("query_balance_list", request)
    try:
      key_file = get_key_file(request.market_type,
                              request.exchange,
                              request.owner,
                              request.api_version,
                              "view")
      client = order_impl.generate_private_client(request.market_type,
                                                  request.exchange,
                                                  request.api_version,
                                                  key_file,
                                                  proxy)
      update = client.query_account_balance()
      update.msg = clear_zero_balance(update.msg)
      query_timestamp = update.req_timestamp
      result = DBOutputBalance(account_id=None,
                               query_timestamp=query_timestamp,
                               balance=update.msg,
                               balance_hid=None)
    except Exception as e:
      print("Skipping query balance", request, type(e), e)
      if FLAGS.traceback:
        print(traceback.format_exc())
      result = None
    finally:
      if FLAGS.verbose:
        print("query_balance_list", result)
      return result

  response_list = multithread_request_executor(f, request_list)
  return response_list


def query_position_list(request_list, proxy=None):
  def f(request):
    if FLAGS.verbose:
      print("query_position_list", request)
    try:
      key_file = get_key_file(request.market_type,
                              request.exchange,
                              request.owner,
                              request.api_version,
                              "view")
      client = order_impl.generate_private_client(request.market_type,
                                                  request.exchange,
                                                  request.api_version,
                                                  key_file,
                                                  proxy)
      update = client.query_account_position()
      update.msg = clear_zero_position(update.msg)
      query_timestamp = update.req_timestamp
      result = DBOutputPosition(account_id=None,
                                query_timestamp=query_timestamp,
                                position=update.msg,
                                position_hid=None)
    except Exception as e:
      print("Skipping query position", request, type(e), e)
      if FLAGS.traceback:
        print(traceback.format_exc())
      result = None
    finally:
      if FLAGS.verbose:
        print("query_position_list", result)
      return result

  response_list = multithread_request_executor(f, request_list)
  return response_list


def query_transfer_list(request_list, currency, proxy=None):
  def f(request):
    if FLAGS.verbose:
      print("query_transfer_list", request)
    try:
      key_file = get_key_file(request.market_type,
                              request.exchange,
                              request.owner,
                              request.api_version,
                              "view")
      client = order_impl.generate_private_client(request.market_type,
                                                  request.exchange,
                                                  request.api_version,
                                                  key_file,
                                                  proxy)
      update = client.query_account_transfer(currency)
      result = DBOutputTransfer(query_timestamp=update.req_timestamp, transfer=update.msg)
    except Exception as e:
      print("Skipping query transfer", request, type(e), e)
      if FLAGS.traceback:
        print(traceback.format_exc())
      result = None
    finally:
      if FLAGS.verbose:
        print("query_transfer_list", result)
      return result

  response_list = multithread_request_executor(f, request_list)
  return response_list
